<template>
  <div>
    <p>
      <button @click="add()" class="btn btn-white btn-default btn-round">
        <i class="ace-icon fa fa-edit"></i>
        新增
      </button>
      &nbsp;&nbsp;&nbsp;&nbsp;
      <button @click="list(1)" class="btn btn-white btn-default btn-round">
        <i class="ace-icon fa fa-refresh red2"></i>
        刷新
      </button>
    </p>
  <table id="simple-table" class="table  table-bordered table-hover">
        <thead>
        <tr>
            <#list fieldList as field>
                <#if field.nameHump!="createdAt" && field.nameHump!="updatedAt">
                    <th>${field.nameCn}</th>
                </#if>
            </#list>
          <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="${entity} in ${entity}s">
            <#list fieldList as field>
                <#if field.nameHump!="createdAt" && field.nameHump!="updatedAt">
                    <#if field.enums>
                        <td>{{${field.enumsConst} | optionKV(${entity}.${field.nameHump})}}</td>
                    <#else>
                        <td>{{${entity}.${field.nameHump}}}</td>
                    </#if>
                </#if>
            </#list>
          <td>
            <div class="hidden-sm hidden-xs btn-group">
              <button @click="update(${entity})" class="btn btn-xs btn-info">
                <i class="ace-icon fa fa-pencil bigger-120"></i>
              </button>
              <button @click="del(${entity}.id)" class="btn btn-xs btn-danger">
                <i class="ace-icon fa fa-trash-o bigger-120"></i>
              </button>
            </div>

            <div class="hidden-md hidden-lg">
              <div class="inline pos-rel">
                <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
                  <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
                </button>

                <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                  <li>
                    <a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
																			<span @click="update(${entity})" class="green">
																				<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
																			</span>
                    </a>
                  </li>

                  <li>
                    <a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
																			<span @click="del(${entity}.id)" class="red">
																				<i class="ace-icon fa fa-trash-o bigger-120"></i>
																			</span>
                    </a>
                  </li>
                </ul>
              </div>
            </div>
          </td>
        </tr>
        </tbody>
      </table>
    <pagination ref="pagination" v-bind:list="list" v-bind:itemCount="8"></pagination>

    <div id="form-modal" class="modal fade" tabindex="-1" role="dialog">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">新增表单</h4>
          </div>
          <div class="modal-body">
            <form class="form-horizontal">
                <#list fieldList as field>
                    <#if field.name!="id" && field.nameHump!="createdAt" && field.nameHump!="updatedAt">
                        <#if field.enums>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">${field.nameCn}</label>
                                <div class="col-sm-10">
                                    <select v-model="${entity}.${field.nameHump}" class="form-control">
                                        <option v-for="o in ${field.enumsConst}" v-bind:value="o.key">{{o.value}}</option>
                                    </select>
                                </div>
                            </div>
                        <#else>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">${field.nameCn}</label>
                                <div class="col-sm-10">
                                    <input v-model="${entity}.${field.nameHump}" class="form-control">
                                </div>
                            </div>
                        </#if>
                    </#if>
                </#list>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            <button @click="insert" type="button" class="btn btn-primary">保存</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
    import Pagination from "../../components/pagination";
    export default {
        components: {Pagination},
        name: "${module}-${entity}",
        data: function () {
          return{
              ${entity}:{},
            ${entity}s: [],
            <#list fieldList as field>
            <#if field.enums>
            ${field.enumsConst}: ${field.enumsConst},
            </#if>
            </#list>
          }
        },
        mounted() {
            let _this = this;
            _this.$refs.pagination.size = 5;
            _this.list(1);
        },
        methods: {
            /**
             * 点击新增
             */
            add() {
                let _this = this;
                _this.${entity} = {};
                $("#form-modal").modal("show");
            },

            /**
             * 点击查询
             * @param page
             */
            list(page) {
                let _this = this;
                Loading.show();
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/${module}/admin/${entity}/list', {
                    page: page,
                    //.$refs获取子组件的变量 . 子组件
                    size: _this.$refs.pagination.size,
                }).then((response) => {
                    //我们应该是要的是data下面的数据
                    Loading.hide();
                    //为了简写
                    let resp = response.data;
                    _this.${entity}s = resp.content.list;
                    //重新渲染render
                    _this.$refs.pagination.render(page, resp.content.total);
                })
            },

            /**
             * 点击增加
             */
            insert() {
                let _this = this;
                // 保存校验
                if (1 != 1
                    <#list fieldList as field>
                    <#if field.name!="id" && field.nameHump!="createdAt" && field.nameHump!="updatedAt" && field.nameHump!="sort">
                    <#if !field.nullAble>
                    || !Validator.require(_this.${entity}.${field.nameHump}, "${field.nameCn}")
                    </#if>
                    <#if (field.length > 0)>
                    || !Validator.length(_this.${entity}.${field.nameHump}, "${field.nameCn}", 1, ${field.length?C})
                    </#if>
                    </#if>
                    </#list>
                ) {
                    return;
                }
                Loading.show();
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/${module}/admin/${entity}/add', _this.${entity}).then((response) => {
                    let resp = response.data;
                    Loading.hide();
                    if (resp.success) {
                        $("#form-modal").modal("hide");
                        _this.list(1);
                        Toast.success("保存成功");
                    }else{
                        Toast.error(resp.message)
                    }
                })
            },

            /**
             * 点击修改
             * @param ${entity}
             */
            update(${entity}) {
                let _this = this;
                _this.${entity} = $.extend({}, ${entity});
                $("#form-modal").modal("show");

            },

            /**
             * 点击删除
             * @param id
             */
            del: function (id) {
                let _this = this;
                Loading.show();
                Confirm.show("删除后不可恢复，确认删除？",function () {
                    Loading.hide();
                    _this.$ajax.delete(process.env.VUE_APP_SERVER+ '/${module}/admin/${entity}/delete/' + id).then((response) => {
                        let resp = response.data;
                        if (resp.success) {
                            _this.list(1);
                            Toast.success("删除成功")
                        }
                    })
                })
            }
          }
        }
</script>
